//
//  JYTJiaxiaojlViewController.m
//  JYT_IOS
//
//  Created by nxa-tankeke on 17/1/10.
//  Copyright © 2017年 nxa-yuanguoxiang. All rights reserved.
//

#import "JYTJiaxiaojlViewController.h"
#import "JYTAppointTableViewCell.h"
#import <MJRefresh.h>
#import "JYTCoachHandler.h"
#import "JYTMineUserClass.h"
#import "School.h"

@interface JYTJiaxiaojlViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic,strong) UITableView *coachList;
@property (nonatomic, strong)NSMutableArray * coaches;

@property (nonatomic, assign)NSInteger page;
@property (nonatomic, strong)NSDictionary * parms;

@end

@implementation JYTJiaxiaojlViewController

-(NSMutableArray *)coaches
{
    if (_coaches == nil) {
        _coaches = [NSMutableArray array];
    }
    
    return _coaches;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    NSDictionary *dic = @{@"lftBtnBgImg":@"ic_goback",
                          @"lftBtnTitle":@"no",
                          @"pageTitle":@"驾校教练",
                          @"rhtBtnBgImg":@"no",
                          @"rhtBtnTitle":@"no"};
    
    [self setNavgationBarWithDic:dic];
    
    [self setupTableView];
    [self setupRefresh];
}

- (void)setupTableView
{
    UITableView *coachList = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, CURRENT_SCREEN_WIDTH, CURRENT_SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
    coachList.dataSource = self;
    coachList.delegate = self;
    coachList.backgroundColor = [UIColor clearColor];
    coachList.separatorStyle = UITableViewCellSeparatorStyleNone;
    [coachList registerNib:[UINib nibWithNibName:NSStringFromClass([JYTAppointTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"trainCell"];
    self.coachList = coachList;
    [self.view addSubview:coachList];
}
- (void)setupRefresh
{
    self.coachList.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreCoaches)];
    
    self.coachList.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewCoaches)];
    
    self.coachList.mj_header.automaticallyChangeAlpha = YES;
    
    [self.coachList.mj_header beginRefreshing];
    
    
}

- (void)loadMoreCoaches
{
    [self.coachList.mj_header endRefreshing];
    
    NSInteger page = self.page + 1;
    NSString *pageStr = [NSString stringWithFormat:@"%ld", page];
    
    NSMutableDictionary *parms = [NSMutableDictionary dictionary];
    [parms setObject:_school.drivingSchoolId forKey:@"DrivingSchoolId"];
    [parms setObject:pageStr forKey:@"PageNo"];
    [parms setObject:@"10" forKey:@"PageSize"];
    
    self.parms = parms;
    
    [[JYTCoachHandler shared] getSchoolCoachWithParam:parms WithHandler:^(NSArray *coaches, NSString *pageNo) {
        // [self.coaches addObjectsFromArray:coaches];
        
        if (self.parms != parms) {
            return ;
        }
        
        if (coaches.count > 0) {
            
            [self.coaches addObjectsFromArray:coaches];
            
            [self.coachList reloadData];
            
            [self.coachList.mj_footer endRefreshing];
            
            self.page = page;
            
        } else {
 
        if (self.parms != parms) {
            return ;
        }
        
        [self.coachList.mj_footer endRefreshing];
        }
    }];

}


- (void)loadNewCoaches
{
    [self.coachList.mj_footer endRefreshing];
   
    NSMutableDictionary *parms = [NSMutableDictionary dictionary];
    [parms setObject:_school.drivingSchoolId forKey:@"DrivingSchoolId"];
    [parms setObject:@"0" forKey:@"PageNo"];
    [parms setObject:@"10" forKey:@"PageSize"];
    
     self.parms = parms;
    [[JYTCoachHandler shared] getSchoolCoachWithParam:parms WithHandler:^(NSArray *coaches, NSString *pageNo) {
       // [self.coaches addObjectsFromArray:coaches];
        
        if (self.parms != parms) {
            return ;
        }
        
        if (coaches.count > 0) {
            
            [self.coaches removeAllObjects];
            
            [self.coaches addObjectsFromArray:coaches];
            
           [self.coachList reloadData];
            
            [self.coachList.mj_header endRefreshing];
            
            self.page = 0;
            
        } else {
            if (self.parms != parms) {
                return ;
            }
            
            [self.coachList.mj_header endRefreshing];
        }

        
    }];
    
    
}


#pragma mark---UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    self.coachList.mj_footer.hidden = (self.coaches.count == 0);
    return self.coaches.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    JYTAppointTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"trainCell"];
    
    cell.model = self.coaches[indexPath.row];
    
    return cell;
}
#pragma mark---UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 140;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
